﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace QuotationService.HistoryQuotation
{
    /// <summary>
    /// 历史数据归档类型
    /// </summary>
    internal class QuotationArchiverType
    {
        /// <summary>
        /// 归档状态统计类型
        /// </summary>
        public static readonly QuotationArchiverType ArchiverStatus = new QuotationArchiverType("ArchiverStatus");

        /// <summary>
        /// 基本信息归档类型
        /// </summary>
        public static readonly QuotationArchiverType BasicInfo = new QuotationArchiverType("BasicInfo");

        /// <summary>
        /// 行情信息归档类型
        /// </summary>
        public static readonly QuotationArchiverType QuotationInfo = new QuotationArchiverType("QuotationInfo");

        /// <summary>
        /// Tick信息归档类型
        /// </summary>
        public static readonly QuotationArchiverType TickInfo = new QuotationArchiverType("TickInfo");

        /// <summary>
        /// 日线数据归档类型
        /// </summary>
        public static readonly QuotationArchiverType DailyData = new QuotationArchiverType("DailyData");

        /// <summary>
        /// 分线数据归档类型
        /// </summary>
        public static readonly QuotationArchiverType MinuteData = new QuotationArchiverType("MinuteData");

        /// <summary>
        /// 日线数据统计类型
        /// </summary>
        public static readonly QuotationArchiverType DailyDataStatistics = new QuotationArchiverType("DailyDataStatistics");

        /// <summary>
        /// 分线数据统计类型
        /// </summary>
        public static readonly QuotationArchiverType MinuteDataStatistics = new QuotationArchiverType("MinuteDataStatistics");

        /// <summary>
        /// 数据类型前缀
        /// </summary>
        private string Prefix;

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="prefix">数据类型前缀</param>
        [DebuggerNonUserCode]
        internal QuotationArchiverType(string prefix)
        {
            this.Prefix = prefix;
        }

        /// <summary>
        /// 运算符
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [DebuggerNonUserCode]
        public static implicit operator string(QuotationArchiverType type)
        {
            return type.Prefix;
        }

        /// <summary>
        /// 运算符
        /// </summary>
        /// <param name="prefix"></param>
        /// <returns></returns>
        [DebuggerNonUserCode]
        public static implicit operator QuotationArchiverType(string prefix)
        {
            if (string.IsNullOrEmpty(prefix) || prefix.Length != 2)
                return null;

            return new QuotationArchiverType(prefix.Substring(0, 2));
        }

        /// <summary>
        /// 获取hash代码
        /// </summary>
        /// <returns></returns>
        [DebuggerNonUserCode]
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// 比较是否相等
        /// </summary>
        /// <param name="obj">比较对象</param>
        /// <returns></returns>
        [DebuggerNonUserCode]
        public override bool Equals(object obj)
        {
            if (obj is string)
                return this == (QuotationArchiverType)(string)obj;

            return this == obj as QuotationArchiverType;
        }

        /// <summary>
        /// 相等运算符
        /// </summary>
        /// <param name="qat1"></param>
        /// <param name="qat2"></param>
        /// <returns></returns>
        [DebuggerNonUserCode]
        public static bool operator ==(QuotationArchiverType qat1, QuotationArchiverType qat2)
        {
            if (ReferenceEquals(qat1, qat2))
                return true;

            if ((object)qat1 == null || (object)qat2 == null)
                return false;

            return string.Compare(qat1.Prefix, qat2.Prefix, true) == 0;
        }

        /// <summary>
        /// 不相等运算符
        /// </summary>
        /// <param name="qat1"></param>
        /// <param name="qat2"></param>
        /// <returns></returns>
        [DebuggerNonUserCode]
        public static bool operator !=(QuotationArchiverType qat1, QuotationArchiverType qat2)
        {
            return !(qat1 == qat2);
        }

        /// <summary>
        /// 转换成字符串
        /// </summary>
        /// <returns></returns>
        [DebuggerNonUserCode]
        public override string ToString()
        {
            return this.Prefix;
        }
    }

}
